using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using CustomNAntTasks.ASCmd;
using DeployReports.Types;
using NAnt.Core;
using NAnt.Core.Attributes;

/*   [-S server[:port][\instance]]
       || http[s]://server:port/<vdir>/msmdpump.dll
  [-U domain\username]   [-P password]
  [-d database]          [-xc extended-connect-string]
  [-i input-file]        [-o output-file]
  [-t query-timeout]     [-tc connect-timeout]
  [-T trace-file]        [-Tt trace-timeout]
  [-Tf text|csv]         [-Td delim-char]
  [-Tl high|medium|low|duration|duration-result]
  [-v var=value...]
  [-Q "cmdline XMLA script, MDX query or DMX statement"
  [-? show syntax summary]
*/

namespace DeployReports.CustomTasks
{
	[TaskName("ascmd")]
	class ASCmdTask : Task
	{
		private string dataSource;
		private FileInfo scriptFile;
		private AuthenticationElement authenticationElement;

		[BuildElement("authentication", Required = false)]
		public AuthenticationElement AuthenticationElement
		{
			get { return authenticationElement; }
			set { authenticationElement = value; }
		}

		[TaskAttribute("datasource", Required = true)]
		public string DataSource
		{
			get { return dataSource; }
			set { dataSource = value; }
		}

		[TaskAttribute("file", Required = true)]
		public FileInfo ScriptFile
		{
			get { return scriptFile; }
			set { scriptFile = value; }
		}

		///<summary>
		///Executes the task.
		///</summary>
		///
		protected override void ExecuteTask()
		{
			List<string> args = new List<string>();
			Array.ForEach(new String[] { "-U", DataSource, "-i", ScriptFile.FullName }, 
				delegate(string s){args.Add(s);});

			if (null!=authenticationElement)
			{
				args.Add("-U");
				args.Add(authenticationElement.UserName);
				args.Add("-P");
				args.Add(authenticationElement.Password);
			}
			ASCmdImpl.Main(new string[] {"-U", DataSource, "-i", ScriptFile.FullName});
		}
	}
}
